package com.tengju.data.infrastructure.repository;

import com.google.common.collect.Lists;
import com.tengju.data.domain.model.report.DataAchievementBonusDateConfigRepository;
import com.tengju.data.domain.model.report.DataAchivevmentBonusDateConfig;
import com.tengju.data.domain.model.report.DateConfigTypeEnum;
import com.tengju.data.infrastructure.persistence.mybatis.dataobject.DataAchievementBonusDateConfigDO;
import com.tengju.data.infrastructure.persistence.mybatis.mapper.tengju.custom.DataAchievementBonusDateConfigMapper;
import com.tengju.data.infrastructure.repository.convert.DataAchievementBonusDateConfigConvert;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Repository
public class DataAchevementBonusDateConfigRepositoryImpl implements DataAchievementBonusDateConfigRepository {

    @Resource
    DataAchievementBonusDateConfigMapper dataAchievementBonusDateConfigMapper;


    @Override
    public DataAchivevmentBonusDateConfig getByDataKeyAndConfigType(String dataKey, DateConfigTypeEnum typeEnum) {
        if (StringUtils.isBlank(dataKey) || typeEnum == null) {
            return null;
        }

        DataAchievementBonusDateConfigDO dateConfigDO =
                dataAchievementBonusDateConfigMapper.getByDataKeyAndConfigType(dataKey, typeEnum.getType());

        return DataAchievementBonusDateConfigConvert.toModel(dateConfigDO);
    }

    @Override
    public List<DataAchivevmentBonusDateConfig> getByConfigTypeAndCurDate(DateConfigTypeEnum typeEnum, LocalDate curDate) {
        if (typeEnum == null) {
            return Lists.newArrayList();
        }
        List<DataAchievementBonusDateConfigDO> doList =
                dataAchievementBonusDateConfigMapper.getByConfigTypeAndCurDate(typeEnum.getType(),curDate.toString());
        if (CollectionUtils.isEmpty(doList)) {
            return Lists.newArrayList();
        }
        return doList.stream()
                .filter(Objects::nonNull)
                .map(DataAchievementBonusDateConfigConvert::toModel)
                .collect(Collectors.toList());
    }
}
